package cn.jy.nvr.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.ProviderAssay;
import cn.jy.nvr.domain.ProviderBaseInfo;
import cn.jy.nvr.domain.ProviderBodyCheck;
import cn.jy.nvr.domain.ProviderChestCheck;
import cn.jy.nvr.domain.ProviderCollection;
import cn.jy.nvr.domain.XY_BASE_COMMUNRECORD;
import cn.jy.nvr.domain.XY_BASE_FIVERELDONNUR;
import cn.jy.nvr.domain.XY_BASE_RELDONNUR;
import cn.jy.nvr.mapper.ProviderAssayMapper;
import cn.jy.nvr.mapper.ProviderBaseInfoMapper;
import cn.jy.nvr.mapper.ProviderBodyCheckMapper;
import cn.jy.nvr.mapper.ProviderChestCheckMapper;
import cn.jy.nvr.mapper.ProviderCollectionMapper;
import cn.jy.nvr.mapper.XY_BASE_COMMUNRECORDMapper;
import cn.jy.nvr.mapper.XY_BASE_FIVERELDONNURMapper;
import cn.jy.nvr.mapper.XY_BASE_RELDONNURMapper;
import cn.jy.nvr.service.NewProviderNotCollectionQuartzJob;
import cn.jy.nvr.utils.DateTools;
/**
 * @author Administrator
 *用来查询今日新人是否献浆，如果是建档了未献浆就生成回访记录   建档未完成的也生成
 *加了一个方法统计浆员采集层数比较高的
 */
@Service("NewProviderNotCollectionQuartzJob")
public class NewProviderNotCollectionQuartzJobImpl  implements NewProviderNotCollectionQuartzJob{

	@Autowired
	ProviderBaseInfoMapper baseInfoMapper;
	
	@Autowired
	ProviderCollectionMapper providerCollectionMapper;
	
	@Autowired
	ProviderBodyCheckMapper bodyCheckMapper;
	
	@Autowired
	ProviderAssayMapper providerAssayMapper;
	@Autowired
	ProviderChestCheckMapper   providerChestCheckMapper;
	@Autowired
	XY_BASE_RELDONNURMapper  xy_BASE_RELDONNURMapper;
	@Autowired
	XY_BASE_FIVERELDONNURMapper xy_base_FIVERELDONNURMapper;
	
	@Autowired
	XY_BASE_COMMUNRECORDMapper  xy_base_communrecordmapper;
	Logger logger=Logger.getLogger(NewProviderNotCollectionQuartzJobImpl.class);
	@Override
	public void work() {
	   String nowDate=new SimpleDateFormat("yyyy-MM-dd").format( new Date());
	   String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
		//先拿到今日等建档的人
		List<ProviderBaseInfo> providerBaseList = baseInfoMapper.getProviderBaseInfoByCreateRQ(nowDate);
		
		List<ProviderBaseInfo> notCollectionList=  new ArrayList<ProviderBaseInfo>();
		//然后建档的人是否献浆
		
		if (providerBaseList.size()>0) {
			
			for (ProviderBaseInfo providerBaseInfo : providerBaseList) {
				
				logger.info("今日新人"+providerBaseInfo.getXM()+" "+providerBaseInfo.getCARDID());
				  String cardid = providerBaseInfo.getCARDID();
				  DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
				  ProviderCollection providerCollectionByGJZIDFromJingYing = providerCollectionMapper.getProviderCollectionByGJZIDFromJingYing(cardid, nowDate);
				  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				  if (providerCollectionByGJZIDFromJingYing==null) {
					  logger.info("今日新人"+providerBaseInfo.getXM()+" "+providerBaseInfo.getCARDID()+"未献浆");
					  notCollectionList.add(providerBaseInfo);
				}
			}
		}
		
		//然后看没献浆且体检或者化检都合格的
		if (notCollectionList.size()>0) {
			
			   Iterator<ProviderBaseInfo> providerBaseInfoIter = notCollectionList.iterator();  
		        while (providerBaseInfoIter.hasNext()) {  
		        	ProviderBaseInfo providerBaseInfo = providerBaseInfoIter.next();
		        	//先去查询体检是否合格
					  String id = providerBaseInfo.getID();
					  if (id==null||"".equals(id)) {
						  break;
					    }
					  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					  ProviderBodyCheck loadBodyCheckByGIZID = bodyCheckMapper.loadBodyCheckByGIZID(id,nowDate);
				    //查询化检
					  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					  ProviderAssay loadProviderAssayByGJZID = providerAssayMapper.loadProviderAssayByGJZID(id, nowDate);
					//查询胸片
					 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					ProviderChestCheck loadProviderChestCheckByGJZID = providerChestCheckMapper.loadProviderChestCheckByGJZID(id,nowDate);
					
					if (loadBodyCheckByGIZID!=null&&"0".equals(loadBodyCheckByGIZID.getHJJG())) {
						  logger.info("今日新人"+providerBaseInfo.getXM()+" "+providerBaseInfo.getCARDID()+"体检不合格");
						providerBaseInfoIter.remove();
					}
					
					if (loadProviderAssayByGJZID!=null&&"0".equals(loadProviderAssayByGJZID.getHJJG())) {
						 logger.info("今日新人"+providerBaseInfo.getXM()+" "+providerBaseInfo.getCARDID()+"化检不合格");
						providerBaseInfoIter.remove();
					}
					
					if (loadProviderChestCheckByGJZID!=null&&"0".equals(loadProviderChestCheckByGJZID.getSFTG())) {
						 logger.info("今日新人"+providerBaseInfo.getXM()+" "+providerBaseInfo.getCARDID()+"胸片不合格");
						providerBaseInfoIter.remove();
					}
					
		        } 
			
		}
		
		//现在notCollectionList里面的人都是要进行生成回访任务的，生成回访任务是 的护士是个根据链接护士     五部链接护士    血源部护士的规则
		if (notCollectionList.size()>0) {
		//遍历这个notCollectionList
			for (ProviderBaseInfo providerBaseInfo : notCollectionList) {
			     //根据ID去找血源链接
				String nurse="";
				String jyxm = providerBaseInfo.getID();
				if (jyxm==null||"".equals(jyxm)) {
					break;
				}
				  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				XY_BASE_RELDONNUR reldonnurByjyid = xy_BASE_RELDONNURMapper.loadXY_BASE_RELDONNURByJYID(jyxm);
				if (reldonnurByjyid!=null&&reldonnurByjyid.getHSXM()!=null&&!"".equals(reldonnurByjyid.getHSXM())) {
					nurse=reldonnurByjyid.getHSXM();
				}else {
					XY_BASE_FIVERELDONNUR xy_base_fivereldonnur=new XY_BASE_FIVERELDONNUR();
					xy_base_fivereldonnur.setJYXM(jyxm);
					  DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
					List<XY_BASE_FIVERELDONNUR> fivereldonnurList = xy_base_FIVERELDONNURMapper.loadXY_BASE_FIVERELDONNUR(xy_base_fivereldonnur);
					if (fivereldonnurList.size()>0) {
						XY_BASE_FIVERELDONNUR xy_base_fivereldonnur2 = fivereldonnurList.get(0);
						  if (xy_base_fivereldonnur2.getHSXM()!=null&&!"".equals(xy_base_fivereldonnur2.getHSXM())) {
							nurse=xy_base_fivereldonnur2.getHSXM();
						} 
					}else {
						String[] nurseList={"mxz28","mxz33","mxz12"};
					     Random random = new Random();
					     int nextInt = random.nextInt(2);
					     nurse=nurseList[nextInt];
					}
				}
				
				XY_BASE_COMMUNRECORD xy_base_communrecord=new XY_BASE_COMMUNRECORD();
				
				xy_base_communrecord.setSCSJ(DateTools.getInstance().dateFormat(new Date(), "yyyy-MM-dd HH:mm:ss"));
				xy_base_communrecord.setJYXM(jyxm);
				xy_base_communrecord.setHSXM(nurse);
				if (providerBaseInfo.getCARDID().startsWith("R")) {
					xy_base_communrecord.setCOMMUNICATIONTYPE("1");
				}else {
					xy_base_communrecord.setCOMMUNICATIONTYPE("2");
				}
				 logger.info("今日新人"+providerBaseInfo.getXM()+" "+providerBaseInfo.getCARDID()+"生成回访记录");
				xy_base_communrecordmapper.save(xy_base_communrecord);
				
				
			}
		}
		//统计采集层数比较高的
		ProviderCollectionCycles();
	}
	public void ProviderCollectionCycles() {
		//首先拿到今年进站的人人员卡号
		logger.info("统计浆员采浆层数");
		
	}
   
}
